package FrontOffice;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;

import javax.swing.JPasswordField;
import javax.swing.WindowConstants;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import javax.swing.JTextField;

public class RegisterNEW extends javax.swing.JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField Benutzername;
	private JPasswordField Passwort;
	private JTextField Vorname;
	private JTextField Name;
	private JSeparator jSeparator2;
	private JLabel Jfehler;
	private JButton jLogin;
	private JSeparator jSeparator5;
	private JButton Register;
	private JButton zurueck;
	private JLabel jLabel4;
	private JLabel jLabel3;
	private JLabel jLabel2;
	private JLabel jLabel1;

	/**
	 * Auto-generated main method to display this JPanel inside a new JFrame.
	 */
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new RegisterNEW());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}

	public RegisterNEW() {
		super();
		initGUI();
	}

	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(800, 600));
			this.setSize(800, 600);
			this.setLayout(null);

			KeyListener spion = new KeyListener() {

				@Override
				public void keyTyped(KeyEvent e) {

				}

				@Override
				public void keyReleased(KeyEvent e) {
					if (e.getKeyCode() == KeyEvent.VK_ENTER
							&& Register.isVisible()) {
						RegisterActionPerformed(null);
					} else if (e.getKeyCode() == KeyEvent.VK_ENTER
							&& Register.isVisible() == false) {
						jLoginActionPerformed(null);
					}
				}

				@Override
				public void keyPressed(KeyEvent e) {

				}
			};
			{
				Benutzername = new JTextField();
				this.add(Benutzername);
				Benutzername.setBounds(480, 111, 196, 28);
			}
			{
				Name = new JTextField();
				this.add(Name);
				Name.setBounds(480, 181, 196, 28);
			}
			{
				Vorname = new JTextField();
				this.add(Vorname);
				Vorname.setBounds(480, 250, 196, 28);
			}
			{
				Passwort = new JPasswordField();
				this.add(Passwort);
				Passwort.setBounds(480, 322, 196, 28);
			}
			{
				jLabel1 = new JLabel();
				this.add(jLabel1);
				jLabel1.setText("Benutzername");
				jLabel1.setBounds(165, 117, 128, 16);
			}
			{
				jLabel2 = new JLabel();
				this.add(jLabel2);
				jLabel2.setText("Name");
				jLabel2.setBounds(165, 187, 128, 16);
			}
			{
				jLabel3 = new JLabel();
				this.add(jLabel3);
				jLabel3.setText("Vorname");
				jLabel3.setBounds(165, 256, 128, 16);
			}
			{
				jLabel4 = new JLabel();
				this.add(jLabel4);
				jLabel4.setText("Passwort");
				jLabel4.setBounds(165, 328, 128, 16);
			}
			{
				Register = new JButton();
				this.add(Register);
				Register.setText("registrieren");
				Register.setBounds(520, 448, 180, 72);
				Register.addActionListener(new ActionListener() {
					
					public void actionPerformed(ActionEvent evt) {
						RegisterActionPerformed(evt);
					}
				});
			}
			{
				zurueck = new JButton();
				this.add(zurueck);
				zurueck.setText("zur\u00FCck");
				zurueck.setBounds(100, 448, 180, 72);
				zurueck.addActionListener(new ActionListener() {
					
					public void actionPerformed(ActionEvent evt) {
						zurueckActionPerformed(evt);
					}

				});
			}
			{
				Jfehler = new JLabel();
				this.add(Jfehler);
				Jfehler.setBounds(313, 405, 250, 22);
			}
			{
				jSeparator2 = new JSeparator();
				this.add(jSeparator2);
				jSeparator2.setBounds(58, 73, 700, 12);
			}
			{
				jSeparator5 = new JSeparator();
				this.add(jSeparator5);
				jSeparator5.setBounds(58, 373, 700, 12);
			}
			{
				jLogin = new JButton();
				this.add(jLogin);
				jLogin.setText("zum Login");
				jLogin.setBounds(520, 448, 180, 72);
				jLogin.setVisible(false);
				jLogin.addActionListener(new ActionListener() {
					
					public void actionPerformed(ActionEvent evt1) {
						jLoginActionPerformed(evt1);
					}
				});
			}
			Benutzername.addKeyListener(spion);
			Name.addKeyListener(spion);
			Vorname.addKeyListener(spion);
			Passwort.addKeyListener(spion);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("deprecation")
	private void RegisterActionPerformed(ActionEvent evt) {

		if (Benutzername.getText().isEmpty() != true
				&& Name.getText().isEmpty() != true
				&& Vorname.getText().isEmpty() != true
				&& Passwort.getText().isEmpty() != true) {
			if (Statements.new_user_anlegen(Benutzername.getText(),
					Name.getText(), Vorname.getText(), Passwort.getText())) {
				Jfehler.setText("Benutzer hinzugef\u00FCgt");
				jLogin.setVisible(true);
				Register.setVisible(false);

			} else
				Jfehler.setText("Benutzername existiert");
		} else
			Jfehler.setText("Bitte \u00FCberpr\u00FCfen Sie Ihre Eingabe");
	}

	private void zurueckActionPerformed(ActionEvent evt) {
		this.removeAll();
		add(new LoginOrRegister());
		this.repaint();
	}

	private void jLoginActionPerformed(ActionEvent evt1) {
		// Start.getStart().getContentPane().removeAll();
		// LoginOrRegister LOR = new LoginOrRegister();
		// Start.getStart().add(LOR);
		// Start.getStart().validate();
		this.removeAll();
		add(new LoginOrRegister());
		this.repaint();
	}

}
